package com.toolkit.util;

import com.alibaba.fastjson2.JSONObject;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author qdl
 * @descripton TODO
 * @Date 2024/1/16 16:57
 */
public class Md5Util {
    private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    /**
     * 空港嘉华-md5加密
     * @param text
     */
    public static String md5(String text) {
        MessageDigest msgDigest = null;
        try {
            msgDigest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException("System doesn't support MD5 algorithm.");
        }
        try {
            msgDigest.update(text.getBytes("utf-8"));
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException("System doesn't support your  EncodingException.");
        }
        byte[] bytes = msgDigest.digest();
        String md5Str = new String(encodeHex(bytes));
        return md5Str;
    }
    private static char[] encodeHex(byte[] data) {
        int l = data.length;
        char[] out = new char[l << 1];

        // two characters form the hex value.
        for (int i = 0, j = 0; i < l; i++) {
            out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
            out[j++] = DIGITS[0x0F & data[i]];
        }
        return out;
    }

    /**
     * 同程商旅-MD5加密
     * @param json
     * @param privateSecret
     * @return
     */
    public static String getSign(JSONObject json, String privateSecret) {
        return Md5Util.stringToMD5(Md5Util.stringToMD5(json.toJSONString()).toLowerCase() + privateSecret).toLowerCase();
    }
    public static String EncoderByMd5(String buf) {
        try {
            MessageDigest digist = MessageDigest.getInstance("MD5");

            byte[] rs = digist.digest(buf.getBytes("UTF-8"));

            StringBuffer digestHexStr = new StringBuffer();

            for (int i = 0; i < 16; i++) {
                digestHexStr.append(byteHEX(rs[i]));

            }

            return digestHexStr.toString();

        } catch (Exception e) {

        }

        return null;

    }
    public static String byteHEX(byte ib) {
        char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

        char[] ob = new char[2];

        ob[0] = Digit[(ib >>> 4) & 0X0F];

        ob[1] = Digit[ib & 0X0F];

        String s = new String(ob);

        return s;

    }
    /**
     * Java自带的MD5加密
     * @param plainText
     * @return
     */
    public static String stringToMD5(String plainText) {
        byte[] secretBytes = null;
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(
                    plainText.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("没有这个md5算法！");
        }
        String md5code = new BigInteger(1, secretBytes).toString(16);
        for (int i = 0; i < 32 - md5code.length(); i++) {
            md5code = "0" + md5code;
        }
        return md5code;
    }
}
